package gameobject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;

import engine.JLeibniz;

/**
 * Represents items that can be taken and used.
 * Will later maybe be an inheritor for equipment. OR WILL IT
 * 
 * @author Simon Dehne
 *
 */
public class Object {
	
	private String name;
	private String description;
	private Map<String, Object> combinations;
	
	// Path to the image file and x/y position
	private String image;
	private int xPos;
	private int yPos;
	
	/**
	 * Constructor, sets the variables.
	 * 
	 * @param name			Name of the object.
	 * @param description	Description of the object.
	 * @param x				x position of the object.
	 * @param y				y position of the object
	 * @throws IOException 
	 */
	public Object(String name, String description, int x, int y) throws IOException {
		this.name = name;
		this.description = description;
		
		this.combinations = new HashMap<String, Object>();
		
		// construct the path to the item image
		this.image = "res/objects/"+name+".png";
		
		ImageIcon imageIc = new ImageIcon(image);
		
		// If no image for this item exits, throw an error
		if(imageIc.getIconHeight() == -1) {
			if(JLeibniz.isDebug) JLeibniz.outWriter.write("No image icon found for item "+name+"! Terminating game.");
			System.exit(-1);
		}		

		/*
		 * Set the position, needs to be passed as a parameter on creation.
		 * Add 30 to x and 50 to y because of the position of the main screen.
		 * 
		 * Also substract the height of the image, because of the way the
		 * coordinate system is laid out.
		 * 
		 * Both of this reduces the need of manual calculating when you
		 * want to add an item - you simply pass the pixel position where the
		 * lower left of the item should be drawn. 
		 */
		this.xPos = x+30;
		this.yPos = y+50-imageIc.getIconHeight();
		
		// get rid of the image icon, since it won't be needed afterwards
		imageIc = null;
	}
	
	/**
	 * Overloaded constructor, used for items that don't need to be displayed
	 * outside of the player's inventory.
	 * 
	 * @param name			Name of the object.
	 * @param description	Description of the object.
	 */
	public Object(String name, String description) {
		this.name = name;
		this.description = description;
		
		// construct the path to the item image
		this.image = "res/objects/"+name+".png";
	}
	
	/**
	 * Getter for the object name.
	 * 
	 * @return	The name.
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Getter for the object description.
	 * 
	 * @return The description.
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Getter for the image path.
	 * 
	 * @return The image path
	 */
	public String getImagePath() {
		return image;
	}
	
	/**
	 * Getter for the x position.
	 * 
	 * @return The x position
	 */
	public int getX() {
		return xPos;
	}
	
	/**
	 * Getter for the y position.
	 * 
	 * @return The y position
	 */
	public int getY() {
		return yPos;
	}
	
	public void addCombination(String target, Object result) {
		this.combinations.put(target, result);
	}
	
	public Map<String, Object> getCombinations() {
		return this.combinations;
	}
	
	/**
	 * Combines two items.
	 * TODO: Shouldn't this be somewhere else..?
	 * 
	 * @param target	The target object.
	 */
	public void combine(Object target) {
		
			/*
			 * If the target item is a valid combination, remove both items
			 * from the players inventory and instead add the new one. Also
			 * display its description.
			 */
		   if(this.combinations.containsKey(target.getName()) && 
				   target.combinations.containsKey(this.name)) {			
			
			   	JLeibniz.getPlayer().removeFromInv(
			   			JLeibniz.getPlayer().getInventory().get(this.name));
			   	JLeibniz.getPlayer().removeFromInv(
			   			JLeibniz.getPlayer().getInventory().get(target.getName()));
			   
			   JLeibniz.getPlayer().addToInv(this.combinations.get(target.getName()));
			   JLeibniz.output(this.combinations.get(target.getName()).getDescription());
		   }
		else {
			JLeibniz.output("I can't combine that.");
		}	
	}
	
	public void setX(int x) {
		this.xPos = x;
	}
	
	public void setY(int y) {
		this.yPos = y;
	}

}
